// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import FastClick from 'fastclick'
import App from './App'
import Router from './router'
import { AjaxPlugin, ToastPlugin, LoadingPlugin, TransferDom, AlertPlugin } from 'vux'
import baseURL from './api'
import utils from './tools/untils'
require('es6-promise').polyfill();

// plugins
Vue.use(LoadingPlugin);
Vue.use(ToastPlugin);
Vue.use(AjaxPlugin);
Vue.use(AlertPlugin)
Vue.directive('transfer-dom', TransferDom)

Vue.prototype.$http.defaults.baseURL = baseURL.dev;
Vue.prototype.$http.defaults.headers['Content-Type'] = 'application/json;charset=UTF-8'
Vue.prototype.$http.defaults.timeout = 5000;

Vue.prototype.$http.interceptors.request.use(config => {
  return config;
}, err => {
  Vue.$vux.alert.show({
    title: "信息",
    content: '请求超时'
  });
  return Promise.resolve(err);
})

Vue.prototype.$http.interceptors.response.use(data => {
  return data;
}, err => {
  if (!err.response) {
    // 断网了  
    Vue.$vux.alert.show({
      title: "信息",
      content: '网络错误'
    });
  }
  if (err.response.status == 504 || err.response.status == 404) {
    Vue.$vux.alert.show({
      title: "信息",
      content: '服务器被吃了⊙﹏⊙∥'
    });
  }else if(err.response.status == 400){
    Vue.$vux.alert.show({
      title: "信息",
      content: '微信认证失败'
    });
  }
  return Promise.resolve(err);
})

const router = Router;

FastClick.attach(document.body)

Vue.config.productionTip = false

Vue.http.get("/wx/getOpenId", {
  params: { 'code': utils.getUrlKey().code }
}).then(function (response) {
  let req = response.data;
  if (req.respCode.code !== "MEHOS-10000") {
    Vue.$vux.alert.show({
      title: "信息",
      content: req.respCode.message
    });
  } else {
    localStorage.setItem("openId", req.wxTokenInfo.openid)
  }
}).catch(function (error) {

});

/* eslint-disable no-new */
new Vue({
  router,
  render: h => h(App)
}).$mount('#app-box')
